﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HomeAutomation.actions;

namespace HomeAutomation.util
{
    class RequestList
    {
        private List<KeyValuePair<DateTime, DeviceAction>> requestTimes = new List<KeyValuePair<DateTime, DeviceAction>>();
        private int nextReady = -1;


        public bool hasNextReady()
        {
            lock (this)
            {
                DateTime now = DateTime.Now;
                for (nextReady = 0; nextReady < requestTimes.Count; ++nextReady)
                {
                    KeyValuePair<DateTime, DeviceAction> cur = requestTimes[nextReady];
                    if (0 < now.CompareTo(cur.Key))
                    {
                        //next ready is at right location
                        return true;
                    }
                }

                nextReady = -1;
                return false;
            }
        }

        public DeviceAction removeReady()
        {
            lock (this)
            {
                if (hasNextReady())
                {
                    KeyValuePair<DateTime, DeviceAction> next = requestTimes[nextReady];
                    requestTimes.RemoveAt(nextReady);
                    return next.Value;
                }
                else
                {
                    return null;
                }
            }
        }

        public void add(DateTime dt, DeviceAction da)
        {
            lock (this)
            {
                requestTimes.Add(new KeyValuePair<DateTime, DeviceAction>(dt, da));
            }
        }
    }
}